//
//  DetailsView.m
//  NewMyMusicAPP
//
//  Created by dlios on 15/6/19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DetailsView.h"

@implementation DetailsView


- (void)dealloc
{
    
    [_scroll release];
    [_updateTime_label release];
    [_regdate_label release];
    [_artistName_label release];
    [_artistAvatar_imageView release];
    [_integral_label release];
    [_totalFavorites_label release];
    [_totalViews_label release];
    [_updateTime_label release];
    [_nickName_label release];
    [_description_label release];
    [_title_label release];
    [_type release];
    [_Id release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame Id:(NSString *)Id type:(NSString *)type
{
    self = [super initWithFrame:frame];
    if (self) {
        self.Id = Id;
        self.type = type;
        [self createView];
    }
    return self;
}


-(void)createView
{
    [self asker];
    
    
    self.line = [[[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT * 55, MYWIDTH, 1)]autorelease];
    self.line.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.3];
   
    
    self.scroll = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, HEIGHT * 56, MYWIDTH, HEIGHT * 296)]autorelease];
    self.scroll.backgroundColor = [UIColor clearColor];
    [self addSubview:self.scroll];
    
    
    self.description_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 15, HEIGHT * 115, WIDTH * 345, HEIGHT * 180)]autorelease];
    self.description_label.backgroundColor = [UIColor clearColor];
   // [self.description_label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    self.description_label.font = [UIFont systemFontOfSize:15];
    self.description_label.numberOfLines = 0;
    self.description_label.textColor = [UIColor whiteColor];
    [self.scroll addSubview:self.description_label];
    
    
}

-(CGFloat)heightFor:(NSString *)content
{
    CGSize size = CGSizeMake( WIDTH * 345, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15.0] forKey:NSFontAttributeName];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}


-(void)asker
{

    NSMutableDictionary *askerDic = [NSMutableDictionary dictionary];
    [askerDic setObject:@"0" forKey:@"D-A"];
    [askerDic setObject:self.Id forKey:@"id"];
    [askerDic setObject:@"true" forKey:@"relatedVideos"];
    [askerDic setObject:@"true" forKey:@"supportHtml"];
    

    if ([self.type isEqual:@"VIDEO"]||[self.type isEqualToString:@"PROGRAM"]) {
        
        [Connect ConnectRequestAFWithURL:VIDEODETAIL params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
            NSMutableDictionary *dic = (NSMutableDictionary *)result;
            
            if (dic == nil) {
                NSLog(@"MV描述 数据没请求下来.不铺数据");
                [self addSubview:self.line];
            }else
            {
                [self video];
                
                self.title_label.text = [dic objectForKey:@"title"];
                self.description_label.text = [dic objectForKey:@"description"];
                
                NSMutableArray *array = [dic objectForKey:@"artists"];
                NSMutableDictionary *newDic = [array firstObject];
                
                //artistAvatar_imageView;//图片
                NSString *photo = [newDic objectForKey:@"artistAvatar"];
                NSURL *url = [NSURL URLWithString:photo];
                [self.artistAvatar_imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_discover_moment@3x.png"]];
                
                self.artistName_label.text = [NSString stringWithFormat:@"作者: %@",[newDic objectForKey:@"artistName"]];
                
                self.totalViews_label.text = [NSString stringWithFormat:@"播放次数: %@",[dic objectForKey:@"totalViews"]];
                
                self.regdate_label.text = [NSString stringWithFormat:@"更新时间: %@",[dic objectForKey:@"regdate"]];
                
                CGFloat height = [self heightFor:self.description_label.text];
                self.scroll.contentSize = CGSizeMake(MYWIDTH, height + 115);
                self.description_label.frame = CGRectMake( WIDTH * 15, HEIGHT * 115, WIDTH * 345, height);
                
                [self addSubview:self.line];
            }
            
            
        }];
    }else if ([self.type isEqual:@"PLAYLIST"]) {
        [Connect ConnectRequestAFWithURL:YueDanDetail params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
            NSMutableDictionary *dic = (NSMutableDictionary *)result;

            
            
            if (dic == nil) {
                NSLog(@"MV描述 数据没请求下来.不铺数据");
            }else
            {
                [self playList];
                self.title_label.text = [dic objectForKey:@"title"];
                self.description_label.text = [dic objectForKey:@"description"];
                
                self.updateTime_label.text = [NSString stringWithFormat:@"更新时间: %@",[dic objectForKey:@"updateTime"]];
                
                self.totalViews_label.text = [NSString stringWithFormat:@"播放次数: %@",[dic objectForKey:@"totalViews"]];
                
                self.totalFavorites_label.text = [NSString stringWithFormat:@"收藏数: %@",[dic objectForKey:@"totalFavorites"]];
                
                self.integral_label.text = [NSString stringWithFormat:@"获取积分: %@",[dic objectForKey:@"integral"]];
                
                NSMutableDictionary *smallDic = [dic objectForKey:@"creator"];
                self.nickName_label.text = [NSString stringWithFormat:@"作者: %@",[smallDic objectForKey:@"nickName"]];
                
                CGFloat height = [self heightFor:self.description_label.text];
                self.scroll.contentSize = CGSizeMake( MYWIDTH, height);
                self.description_label.frame = CGRectMake( WIDTH * 15, HEIGHT * 115, WIDTH * 345, height);
                
                [self addSubview:self.line];

                
                
            }
            
        }];
        
    }
    
    
    
}

-(void)video
{
    UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH, HEIGHT * 100)]autorelease];
    myView.backgroundColor = [UIColor clearColor];
    [self.scroll addSubview:myView];
    
    //artistAvatar_imageView;//图片
    self.artistAvatar_imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 15,HEIGHT * 5,WIDTH * 70,WIDTH * 70)]autorelease];
    self.artistAvatar_imageView.backgroundColor = [UIColor redColor];
    self.artistAvatar_imageView.layer.masksToBounds = YES;//设置圆角边缘
    self.artistAvatar_imageView.layer.cornerRadius = (WIDTH * 70) / 2;//设置弧度
//    self.artistAvatar_imageView.layer.borderWidth = 1;//设置边框宽度
//    self.artistAvatar_imageView.layer.borderColor = [UIColor yellowColor].CGColor;//设置边框颜色
    [myView addSubview:self.artistAvatar_imageView];

    //artistName_label;//作者
    self.artistName_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 90,HEIGHT * 30,WIDTH * 150,HEIGHT * 20)]autorelease];
    self.artistName_label.backgroundColor = [UIColor clearColor];
    self.artistName_label.font = [UIFont systemFontOfSize:13];
    self.artistName_label.textColor = [UIColor lightGrayColor];
    [myView addSubview:self.artistName_label];

    //totalViews_label;//播放次数
    self.totalViews_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 15,HEIGHT * 80,WIDTH * 120,HEIGHT * 20)]autorelease];
    self.totalViews_label.backgroundColor = [UIColor clearColor];
    self.totalViews_label.font = [UIFont systemFontOfSize:13];
    self.totalViews_label.textColor = [UIColor lightGrayColor];
    [myView addSubview:self.totalViews_label];
    
    //regdate_label;//更新时间
    self.regdate_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 180,HEIGHT * 80,WIDTH * 170,HEIGHT * 20)]autorelease];
    self.regdate_label.backgroundColor = [UIColor clearColor];
    self.regdate_label.font = [UIFont systemFontOfSize:13];
    self.regdate_label.textColor = [UIColor lightGrayColor];
    [myView addSubview:self.regdate_label];
    
}



-(void)playList
{
    
    //标题
    self.title_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 15,HEIGHT * 5,WIDTH * 325,HEIGHT * 30)]autorelease];
    self.title_label.backgroundColor = [UIColor clearColor];
    [self.title_label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    self.title_label.textColor = [UIColor whiteColor];
    [self.scroll addSubview:self.title_label];
    
    //作者 nickName
    self.nickName_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 15,HEIGHT * 40,WIDTH * 140,HEIGHT * 20)]autorelease];
    self.nickName_label.backgroundColor = [UIColor clearColor];
    self.nickName_label.font = [UIFont systemFontOfSize:13];
    self.nickName_label.textColor = [UIColor lightGrayColor];
    [self.scroll addSubview:self.nickName_label];
    
    //更新时间 updateTime
    self.updateTime_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 160,HEIGHT * 40,WIDTH * 200,HEIGHT * 20)]autorelease];
    self.updateTime_label.backgroundColor = [UIColor clearColor];
    self.updateTime_label.font = [UIFont systemFontOfSize:13];
    self.updateTime_label.textColor = [UIColor lightGrayColor];
    [self.scroll addSubview:self.updateTime_label];
    
    //播放次数 totalViews
    self.totalViews_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 15,HEIGHT * 65,WIDTH * 240,HEIGHT * 20)]autorelease];
    self.totalViews_label.backgroundColor = [UIColor clearColor];
    self.totalViews_label.font = [UIFont systemFontOfSize:13];
    self.totalViews_label.textColor = [UIColor lightGrayColor];
    [self.scroll addSubview:self.totalViews_label];
    
    //收藏数 totalFavorites
    self.totalFavorites_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 150,HEIGHT * 65,WIDTH * 100,HEIGHT * 20)]autorelease];
    self.totalFavorites_label.backgroundColor = [UIColor clearColor];
    self.totalFavorites_label.font = [UIFont systemFontOfSize:13];
    self.totalFavorites_label.textColor = [UIColor lightGrayColor];
    [self.scroll addSubview:self.totalFavorites_label];
    
    //获得积分 integral
    self.integral_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 260,HEIGHT * 65,WIDTH * 100,HEIGHT * 20)]autorelease];
    self.integral_label.backgroundColor = [UIColor clearColor];
    self.integral_label.font = [UIFont systemFontOfSize:13];
    self.integral_label.textColor = [UIColor lightGrayColor];
    [self.scroll addSubview:self.integral_label];
    

    
}












/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
